const Service = require('egg').Service
class GoodsService extends Service {
  // 新增
  async add({ id, user_id, type_id, name, price, photo, num, descript }) {
    let params = {
      user_id,
      type_id,
      name,
      price,
      photo,
      num,
      descript
    }
    let res = null
    if (id) {
      res = await this.ctx.model.Goods.update(params, { where: { id } })
    } else {
      res = await this.ctx.model.Goods.create(params)
    }
    return res
  }
  // 列表
  async list({ type_id, ids, user_id }) {
    let params = {}
    if (type_id) {
      params.type_id = type_id
    }
    if (ids) {
      params.id = { [this.app.Sequelize.Op.or]: ids }
    }
    if (user_id) {
      params.user_id = user_id
    }
    let res = await this.ctx.model.Goods.findAll({
      where: params,
      order: [['created_at', 'DESC']]
    })
    return res
  }
  // 添加购物车
  async addCart({ goods_id, user_id }) {
    let params = { goods_id, user_id }
    // 判断是否已经加入购物车
    let check_res = await this.ctx.model.Cart.findOne({ where: params })
    let res = null
    if (check_res) {
      res = await this.ctx.model.Cart.update(
        { goods_num: check_res.goods_num + 1, updated_at: new Date() },
        { where: { id: check_res.id } }
      )
    } else {
      res = await this.ctx.model.Cart.create(params)
    }
    return res
  }
  // 获取购物车
  async getCart({ user_id }) {
    // 判断是否已经加入购物车
    let check_res = await this.ctx.model.Cart.findAll({ where: { user_id } })
    let goods_ids = check_res.map((item) => item.goods_id)
    // 获取商品详细信息
    let res = await this.ctx.model.Goods.findAll({
      where: { id: { [this.app.Sequelize.Op.or]: goods_ids } }
    })
    let result = []
    for (let item of check_res) {
      for (let subItem of res) {
        if (item.goods_id == subItem.id) {
          result.push({
            ...subItem.dataValues,
            user_id: item.user_id,
            goods_num: item.goods_num,
            cart_id: item.id
          })
        }
      }
    }
    return result
  }
  // 清理库存
  async clearStock({ goods }, transaction) {
    let task = []
    for (let item of goods) {
      task.push(
        this.ctx.model.query(
          `UPDATE t_goods SET num = num - :goods_num WHERE id = :id`,
          {
            type: 'UPDATE',
            replacements: {
              goods_num: item.goods_num,
              id: item.id
            },
            transaction
          }
        )
      )
    }
    const res = await Promise.all(task)
    return res
  }
  // 清理购物车
  async clearCart({ goods, user_id }) {
    let cart_ids = goods.map((item) => item.cart_id)
    const res = await this.ctx.model.Cart.destroy({
      where: {
        id: { [this.app.Sequelize.Op.or]: cart_ids },
        user_id
      }
    })
    return res
  }
  // 删除商品
  async remove({ id, user_id }) {
    const res = await this.ctx.model.Goods.destroy({
      where: {
        id,
        user_id
      }
    })
    return res
  }
}
module.exports = GoodsService
